package com.platform.modules.mall.activity.restrict;

import com.alibaba.fastjson.JSONObject;
import com.platform.common.utils.Constant;
import com.platform.common.utils.DateUtils;
import com.platform.common.utils.Md5Utils;
import com.platform.modules.mall.activity.entity.bo.ActivityGoshoppingBo;
import com.platform.modules.mall.activity.entity.bo.ActivityResultStatus;
import com.platform.modules.mall.entity.MallActivityEntity;
import com.platform.modules.mall.entity.MallOrderEntity;
import com.platform.modules.mall.entity.MallWhitelistEntity;
import com.platform.modules.mall.service.MallOrderService;
import com.platform.modules.mall.service.MallWhitelistService;
import com.platform.modules.sys.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 白名单相关的限制规则：白名单手机、白名单类型
 *      不关心等级，只关心摆明的数量
 */
public class WhitelistPhoneVerify extends RestrictAbstract{

    @Autowired
    protected MallWhitelistService mallWhitelistService;
    @Autowired
    protected SysConfigService sysConfigService;
    @Autowired
    private MallOrderService mallOrderService;

    /**
     * 验证是否有白名单信息
     * @param activityGoshoppingBo
     * @param mallActivityEntity
     * @return
     */
    @Override
    public boolean verify(ActivityGoshoppingBo activityGoshoppingBo, MallActivityEntity mallActivityEntity) {
        //是否md5加密
        String isMd5= sysConfigService.getValue("WHITELIST_MD5");
        List<MallWhitelistEntity> whitelist= mallWhitelistService.selectWhiteListByPhone(
                Constant.STR_ONE.equals(isMd5)? Md5Utils.encrypt(activityGoshoppingBo.getPhone()):activityGoshoppingBo.getPhone(),
                mallActivityEntity.getWhitelistType() );
        if (whitelist==null||whitelist.size()==0){
            throw ActivityResultStatus.buildException(ActivityResultStatus.Status.NOT_WHITE_LIST);
        }
        for (MallWhitelistEntity s:whitelist){
            if (s.getNumber()>=1&& DateUtils.compare(s.getLapseTime(),new Date(),null)){
                activityGoshoppingBo.setData(s);
                return true;
            }
        }
        /*需要支付的活动，查看是否有未支付订单*/
        if (1==mallActivityEntity.getNeedToPay()){
            List<MallOrderEntity> list = mallOrderService.lambdaQuery()
                    .eq(MallOrderEntity::getUserId,activityGoshoppingBo.getUserId())
//                    .eq(MallOrderEntity::getMobile, activityGoshoppingBo.getPhone())
                    .eq(MallOrderEntity::getPayStatus, 1)
                    .eq(MallOrderEntity::getOrderStatus,0)
                    .eq(MallOrderEntity::getActivityId, mallActivityEntity.getId()).list();
            if (list!=null&&list.size()>0){
                throw ActivityResultStatus.buildException(ActivityResultStatus.Status.ORDER_PAY_NO);
            }
        }

        throw ActivityResultStatus.buildException(ActivityResultStatus.Status.SUCCESS_RECEICE);
    }

    /**
     * 获得白名单的次数
     * @param activityGoshoppingBo
     * @param mallActivityEntity
     * @return
     */
    @Override
    public Integer getNumber(ActivityGoshoppingBo activityGoshoppingBo, MallActivityEntity mallActivityEntity) {
        Object data = activityGoshoppingBo.getData();
        if (data!=null){
            MallWhitelistEntity le =  (MallWhitelistEntity) data;
            return le.getNumber();
        }
        return 0;
    }

    /**
     * 领取成功后，减扣白名单的次数
     * @param bo
     * @return
     */
    @Override
    public boolean successAfter(ActivityGoshoppingBo bo) {
        MallWhitelistEntity le = (MallWhitelistEntity) bo.getData();
        le.setNumber(le.getNumber()-bo.getNumber());
        mallWhitelistService.update(le);
        return true;
    }

    /**
     * 包装返回的数据类型
     * @param activityGoshoppingBo
     * @param mallActivityEntity
     * @return
     */
    @Override
    public JSONObject getDataJson(ActivityGoshoppingBo activityGoshoppingBo, MallActivityEntity mallActivityEntity) {
        JSONObject object =new JSONObject();
        MallWhitelistEntity whitelistEntity = (MallWhitelistEntity) activityGoshoppingBo.getData();
        object.put("level",whitelistEntity.getLevel());
        object.put("number",whitelistEntity.getNumber());
        return object;
    }

    /**
     * 给有效日期最近的白名单增加次数
     * @param activityEntity
     * @param bo
     * @return
     */
    @Override
    public boolean cancel(MallActivityEntity activityEntity, ActivityGoshoppingBo bo) {
        String isMd5= sysConfigService.getValue("WHITELIST_MD5");
        List<MallWhitelistEntity> whitelist= mallWhitelistService.selectWhiteListByPhone(
                Constant.STR_ONE.equals(isMd5)? Md5Utils.encrypt(bo.getPhone()):bo.getPhone(),
                activityEntity.getWhitelistType() );
        if (whitelist==null||whitelist.size()==0){
            throw ActivityResultStatus.buildException(ActivityResultStatus.Status.NOT_WHITE_LIST);
        }
        for (MallWhitelistEntity s:whitelist){
            /*将白名单的数量添加在有效的信息上*/
            if (DateUtils.compare(s.getLapseTime(),new Date(),null)){
                s.setNumber(s.getNumber()+bo.getNumber());
                mallWhitelistService.update(s);
                return true;
            }
        }
        return true;
    }
}
